package cn.iocoder.yudao.module.project.controller.admin.workstation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.project.controller.admin.workstation.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.workstation.WorkstationDO;
import cn.iocoder.yudao.module.project.service.workstation.WorkstationService;

@Tag(name = "管理后台 - 工位")
@RestController
@RequestMapping("/project/workstation")
@Validated
public class WorkstationController {

    @Resource
    private WorkstationService workstationService;

    @PostMapping("/create")
    @Operation(summary = "创建工位")
    @PreAuthorize("@ss.hasPermission('project:workstation:create')")
    public CommonResult<Integer> createWorkstation(@Valid @RequestBody WorkstationSaveReqVO createReqVO) {
        return success(workstationService.createWorkstation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工位")
    @PreAuthorize("@ss.hasPermission('project:workstation:update')")
    public CommonResult<Boolean> updateWorkstation(@Valid @RequestBody WorkstationSaveReqVO updateReqVO) {
        workstationService.updateWorkstation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('project:workstation:delete')")
    public CommonResult<Boolean> deleteWorkstation(@RequestParam("id") Integer id) {
        workstationService.deleteWorkstation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('project:workstation:query')")
    public CommonResult<WorkstationRespVO> getWorkstation(@RequestParam("id") Integer id) {
        WorkstationDO workstation = workstationService.getWorkstation(id);
        return success(BeanUtils.toBean(workstation, WorkstationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工位分页")
    @PreAuthorize("@ss.hasPermission('project:workstation:query')")
    public CommonResult<PageResult<WorkstationRespVO>> getWorkstationPage(@Valid WorkstationPageReqVO pageReqVO) {
        PageResult<WorkstationDO> pageResult = workstationService.getWorkstationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkstationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工位 Excel")
    @PreAuthorize("@ss.hasPermission('project:workstation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkstationExcel(@Valid WorkstationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkstationDO> list = workstationService.getWorkstationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工位.xls", "数据", WorkstationRespVO.class,
                        BeanUtils.toBean(list, WorkstationRespVO.class));
    }

}